import set.DisjointSets;
import java.util.Hashtable;



public class VertexSet extends DisjointSets{
	Hashtable<Object, Integer> vertexInts;

	/**
	 * constructor takes in an array of vertex objects and maps each one to an integer using a hash table.
	 * @param vertices is an array of vertex objects.
	 */
	public VertexSet(Object[] vertices) {
		super(vertices.length);
		vertexInts = new Hashtable<Object, Integer>();
		for (int i = 0; i < vertices.length; i++ ) {
			vertexInts.put(vertices[i],i); 
		}
	}

	
	  /**
	   *  union() unites two disjoint sets into a single set.  A union-by-size
	   *  heuristic is used to choose the new root.  This method will corrupt
	   *  the data structure if root1 and root2 are not roots of their respective
	   *  sets, or if they're identical.
	   *
	   *  @param v1 is a vertex object, converted to an integer root 1, the root of the first set.
	   *  @param v2 is a vertex object, converted to an integer root 2, the root of the other set.
	   **/
	public void union (Object v1, Object v2) {
		int vertex1 = find(v1);
		int vertex2 = find(v2);
		super.union(vertex1, vertex2);
	}
	
	
	  /**
	   *  find() finds the (int) name of the set containing a given element.
	   *  Performs path compression along the way.
	   *
	   *  @param v is the vertex object, which is then converted to an integer.
	   *  @return the set containing x.
	   **/
	public int find(Object v) {
		int vertex = ((Integer) vertexInts.get(v)).intValue();
		return super.find(vertex);
	}
	
}
